function [publicb,bayesianb,actualbeliefs] = findbeliefs(params,action1,action0,price,pricethreshold)
% This function non-parameterically estimates the beliefs that best fit
% behavior in the treatment in which subjects are not given correct
% Bayesian beliefs. Preferences are taken from the treatment in which
% beliefs are provided and used to calculate, for each signal and each public 
% signal difference, the belief that best fits the data on average.

q=0.7;
numtypes = size(params,2);
numsubj = sum(params(3,:));
% preference parameters
delta = (params(1,:)>=0)+(params(1,:)<0).*(params(1,:)+1-1E-6); % delta is 1 for herding/risk-neutral types, else delta-alpha+1
alpha = (params(1,:)<=0)+(params(1,:)>0).*(1-params(1,:)-1E-6); % alpha is 1 for contrarian/risk-neutral types, else 1-(delta-alpha)
lambda = params(2,:);

for p=1:size(pricethreshold,2) % loop over price thresholds
    
    publicb(p) = pricethreshold(p)/100; % actual price/public belief
    
    for j=1:2 % loop over signals
        if j==1
            action = action1;
            bayesianb(p,j) = publicb(p)*q/(publicb(p)*q+(1-publicb(p))*(1-q));
        else
            action = action0;
            bayesianb(p,j) = publicb(p)*(1-q)/(publicb(p)*(1-q)+(1-publicb(p))*q);
        end
        
        % extract actions at public signal difference
        paction = action((100*price>=pricethreshold(p)-0.01)&(100*price<=pricethreshold(p)+0.01));
        fracsell = sum(paction==-1)/size(paction,1);
        fracnt = sum(paction==0)/size(paction,1);
        fracbuy = sum(paction==1)/size(paction,1);
        
        % loop over range of beliefs calculating fractions of predicted
        % actions
        for bidx=-20:1:20
            idx = bidx+21;
            b = min(max(0.01,bayesianb(p,j)+0.01*bidx),0.99);
            avec=[];
            for i = 1:numtypes
                EUbuy = EU_la([b;1-b],[1-publicb(p);-publicb(p)],delta(i),alpha(i),lambda(i));
                EUsell = EU_la([b;1-b],[publicb(p)-1;publicb(p)],delta(i),alpha(i),lambda(i));
                % assume trader follows signal if indifferent 
                % action is 1 for buy, 0 for no trade, -1 for sell
                a = double((EUbuy>=0)&&(EUbuy>=EUsell)) - double((EUsell>0)&&(EUsell>EUbuy));
                % expand to each trader
                avec = [avec a*ones(1,params(3,i))];
            end
            % fraction of each action
            predfracsell(idx) = sum(avec==-1)/numsubj;
            predfracnt(idx) = sum(avec==0)/numsubj;
            predfracbuy(idx) = sum(avec==1)/numsubj;
            barr(idx) = b;
        end
        
        % choose belief that minimizes sum of squares differences in fractions
        errors = (fracsell-predfracsell).^2+(fracnt-predfracnt).^2+(fracbuy-predfracbuy).^2;
        [unused,erridx] = find(errors==min(errors)); % all values that minimize error
        if sum(erridx==1)>0 || sum(erridx==size(errors,2))
            error('Smallest error at boundary - extend range of possible beliefs');
        end
        % set misspecified belief to closest one to Bayesian
        bayesianidx = 21;
        [unused,closestidx] = min(abs(erridx-bayesianidx));
        erridx = erridx(closestidx);
        actualbeliefs(p,j) = barr(erridx);
    end
end

end

